package com.example.demo.controller;

import com.example.demo.utils.treeUtil.TransTreeEntity;
import com.example.demo.utils.treeUtil.TreeUtil;
import lombok.Data;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * 控制层。
 * 工具类测试接口
 *
 * @author Liubw
 * @since 2024-11-13
 */
@SpringBootTest
@RunWith(SpringRunner.class)
public class testController {
    private static final Logger logger = LoggerFactory.getLogger(testController.class);
    @Autowired
    private TreeUtil treeUtil;

    @Data
    public static
    class Entity {
        private String id;
        private String pid;
        private String label;
        private String value;

        public Entity(String id, String pid, String label, String value) {
            this.id = id;
            this.pid = pid;
            this.label = label;
            this.value = value;
        }
    }

    @Test
    public void testTreeUtil() throws Exception {
        logger.info(queryList().toString());
        logger.info(queryDeptStock().toString());
    }

    @GetMapping({"/testUtil/list"})
    public List queryList() {

        List<Entity> flatList = new ArrayList<>();
        // 模拟mapper返回的list
        flatList.add(new Entity("1", "A", "节点1", "1"));
        flatList.add(new Entity("21", "A2", "节点2", "21"));
        flatList.add(new Entity("31", "A3", "节点3", "31"));
        flatList.add(new Entity("41", "A4", "节点4", "41"));

        flatList.add(new Entity("2", "1", "节点1-2", "2"));
        flatList.add(new Entity("3", "1", "节点1-3", "3"));

        flatList.add(new Entity("22", "21", "节点2-2", "22"));
        flatList.add(new Entity("23", "21", "节点2-3", "23"));
        flatList.add(new Entity("32", "31", "节点3-2", "32"));

        TransTreeEntity trans = new TransTreeEntity();
        trans.setId("id");
        trans.setPid("pid");
        trans.setLabel("label");
        trans.setValue("value");
        trans.setRoot("根节点");
        return treeUtil.list2Tree(flatList, trans);
    }

    @GetMapping({"/testUtil"})
    public List queryDeptStock() throws Exception {

        List<Entity> flatList = new ArrayList<>(); // mapper1
        List<Entity> flatList2 = new ArrayList<>(); // mapper2
        List<Entity> flatList3 = new ArrayList<>(); // mapper3
        List<Entity> flatList4 = new ArrayList<>(); // mapper4
        // 模拟多个mapper返回的list
        flatList.add(new Entity("1", "A", "节点1", "1"));
        flatList.add(new Entity("21", "A1", "节点2", "21"));


        flatList2.add(new Entity("3", "1", "节点1-2", "3"));
        flatList2.add(new Entity("2", "1", "节点1-1", "2"));
        flatList2.add(new Entity("22", "21", "节点2-1", "22"));

        flatList3.add(new Entity("4", "2", "节点1-2-1", "4"));
        flatList3.add(new Entity("9", "2", "节点1-2-2", "9"));
        flatList3.add(new Entity("8", "3", "节点1-3-1", "8"));

        flatList4.add(new Entity("6", "4", "节点1-4-1-2", "6"));
        flatList4.add(new Entity("5", "4", "节点1-4-1-1", "5"));
        flatList4.add(new Entity("7", "8", "节点1-8-1-1", "7"));


        TransTreeEntity trans = new TransTreeEntity();
        trans.setId("id");
        trans.setPid("pid");
        trans.setLabel("label");
        trans.setValue("value");
        trans.setRoot("根节点");
        List<List> list = new ArrayList();
        list.add(flatList2);
        list.add(flatList3);
        list.add(flatList4);
        return treeUtil.polymerizeTreeCycle(flatList, list, trans, trans);
    }
}
